﻿﻿#!/bin/bash

# 加载环境变量
source .env

# 备份当前镜像
backup_image() {
    echo "正在备份当前镜像..."
    docker tag ${APP_IMAGE} ${APP_IMAGE}_bak
    if [ $? -eq 0 ]; then
        echo "镜像备份成功"
    else
        echo "镜像备份失败"
    fi
}

while true; do
    echo "请选择操作:"
    echo "1. up: 启动"
    echo "2. update: 更新"
    echo "3. migrate: 迁移"
    echo "4. down: 停止"
    echo "5. rollback: 回滚"
    echo "6. exit: 退出"
    read -p "请输入选项 (1/2/3/4/5/6): " choice

    case $choice in
        1)
            echo "正在启动服务..."
            docker-compose up -d
            ;;
        2)
            echo "正在更新服务..."
            backup_image
            docker-compose pull && docker-compose up -d
            ;;
        3)
            echo "正在迁移数据库..."
            docker-compose pull migrator && docker-compose run --rm migrator
            ;;
        4)
            echo "正在停止服务..."
            docker-compose down
            ;;
        5)
            echo "正在回滚到备份版本..."
            if docker inspect ${APP_IMAGE}_bak > /dev/null 2>&1; then
                echo "回滚到备份镜像: ${APP_IMAGE}_bak"
                docker tag ${APP_IMAGE}_bak ${APP_IMAGE}
                docker-compose up -d
                echo "回滚完成"
            else
                echo "未找到备份镜像"
            fi
            ;;
        6)
            echo "退出脚本"
            break
            ;;
        *)
            echo "无效的选择，请重新输入。"
            ;;
    esac
done
